package com.shop.cloud.controller.message;

import io.swagger.annotations.ApiOperation;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mapper.JsonMapper;
import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.Message;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.service.hongfu.IMessageService;
import com.shop.cloud.service.hongfu.impl.UserService;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

@RestController
@RequestMapping(value = "message")
public class MessageController extends BaseController {

	private static final Logger logger=Logger.getLogger(MessageController.class);
	
	@Autowired
	private IMessageService messageService;
	
	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "获取消息列表", notes = "获取消息列表")
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,@RequestBody Message message){
		message.setStatus(0);
		message.setOrderBy("MESSAGE.create_time DESC");
		message.setType(1);
		Page<Message> datas = messageService.page(message);
		return ResponseUtil.ok(datas);
	}
	
	@ApiOperation(value = "添加消息", notes = "添加消息")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,@RequestBody Message message){
		message.setStatus(0);
		message.setCreateTime(new Timestamp((new Date()).getTime()));
		message.setType(1);
		message.setIsPush(0);
		messageService.insert(message);
		logger.info("消息管理添加:"+ JsonMapper.nonDefaultMapper().toJson(message));
		return ResponseUtil.ok();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(@CurrentUserId Long userId,@RequestBody Message message){
		message.setStatus(1);
		int success = messageService.update(message);
		return ResponseUtil.ok(success);
	}

}